using System;

namespace HooIzDat.Web.Models
{
    public partial class GameState
    {
        public virtual int GameID
        {
            get { return _gameID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_gameID != value)
                    {
                        if (Game != null && Game.GameID != value)
                        {
                            Game = null;
                        }
                        _gameID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private int _gameID;
    
        public virtual int ActivePlayerID
        {
            get { return _activePlayerID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_activePlayerID != value)
                    {
                        if (ActivePlayer != null && ActivePlayer.PlayerID != value)
                        {
                            ActivePlayer = null;
                        }
                        _activePlayerID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private int _activePlayerID;
    
        public virtual string QuestionAsked
        {
            get;
            set;
        }
    
        public virtual Nullable<bool> Answer
        {
            get;
            set;
        }
    
        public virtual int TurnsPlayed
        {
            get;
            set;
        }
    
        public virtual Nullable<int> GuessedCharacterID
        {
            get { return _guessedCharacterID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_guessedCharacterID != value)
                    {
                        if (GuessedCharacter != null && GuessedCharacter.CharacterID != value)
                        {
                            GuessedCharacter = null;
                        }
                        _guessedCharacterID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private Nullable<int> _guessedCharacterID;

        public virtual Character GuessedCharacter
        {
            get { return _guessedCharacter; }
            set
            {
                if (!ReferenceEquals(_guessedCharacter, value))
                {
                    var previousValue = _guessedCharacter;
                    _guessedCharacter = value;
                    FixupGuessedCharacter(previousValue);
                }
            }
        }
        private Character _guessedCharacter;
    
        public virtual Game Game
        {
            get { return _game; }
            set
            {
                if (!ReferenceEquals(_game, value))
                {
                    var previousValue = _game;
                    _game = value;
                    FixupGame(previousValue);
                }
            }
        }
        private Game _game;
    
        public virtual Player ActivePlayer
        {
            get { return _activePlayer; }
            set
            {
                if (!ReferenceEquals(_activePlayer, value))
                {
                    var previousValue = _activePlayer;
                    _activePlayer = value;
                    FixupActivePlayer(previousValue);
                }
            }
        }
        private Player _activePlayer;

        private bool _settingFK = false;
    
        private void FixupGuessedCharacter(Character previousValue)
        {
            if (previousValue != null && previousValue.GameStates.Contains(this))
            {
                previousValue.GameStates.Remove(this);
            }
    
            if (GuessedCharacter != null)
            {
                if (!GuessedCharacter.GameStates.Contains(this))
                {
                    GuessedCharacter.GameStates.Add(this);
                }
                if (GuessedCharacterID != GuessedCharacter.CharacterID)
                {
                    GuessedCharacterID = GuessedCharacter.CharacterID;
                }
            }
            else if (!_settingFK)
            {
                GuessedCharacterID = null;
            }
        }
    
        private void FixupGame(Game previousValue)
        {
            if (previousValue != null && ReferenceEquals(previousValue.GameState, this))
            {
                previousValue.GameState = null;
            }
    
            if (Game != null)
            {
                Game.GameState = this;
                if (GameID != Game.GameID)
                {
                    GameID = Game.GameID;
                }
            }
        }
    
        private void FixupActivePlayer(Player previousValue)
        {
            if (previousValue != null && previousValue.GameStates.Contains(this))
            {
                previousValue.GameStates.Remove(this);
            }
    
            if (ActivePlayer != null)
            {
                if (!ActivePlayer.GameStates.Contains(this))
                {
                    ActivePlayer.GameStates.Add(this);
                }
                if (ActivePlayerID != ActivePlayer.PlayerID)
                {
                    ActivePlayerID = ActivePlayer.PlayerID;
                }
            }
        }
    }
}
